package xcmg.device.service.report;

import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.report.OutboundSummaryDO;
import xcmg.device.dao.mapper.report.OutboundSummaryMapper;
import xcmg.device.infra.ExportUtil;
import xcmg.device.service.vo.report.OutboundSummaryConditionVO;
import xcmg.device.service.vo.report.OutboundSummaryVO;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class OutboundSummaryService {

    @Autowired
    private OutboundSummaryMapper outboundSummaryMapper;

    @Autowired
    private HttpServletResponse response;

    public PageCommonVO pageList(SearchCommonVO<OutboundSummaryConditionVO> conditionVO, String orgId) {
        conditionVO.getFilters().setOrgId(orgId);
        PageHelperPlus.startPage(conditionVO.getPageNum(), conditionVO.getPageSize());
        List<OutboundSummaryDO> outboundSummaryDOs = outboundSummaryMapper.list(conditionVO.getFilters());
        PageCommonVO pageCommonVO = new PageCommonVO();
        pageCommonVO.setPageInfo(new PageInfo(outboundSummaryDOs));
        List<OutboundSummaryVO> outboundSummaryVOs = new ArrayList<OutboundSummaryVO>();
        for (OutboundSummaryDO outboundSummaryDO : outboundSummaryDOs) {
            outboundSummaryDO.setBillAmountWithTax(outboundSummaryDO.getSellAmount());
            if (outboundSummaryDO.getBillAmountWithTax() != null) {
                outboundSummaryDO.setBillAmountWithoutTax(outboundSummaryDO.getBillAmountWithTax().divide(new BigDecimal("1.13"), 2, BigDecimal.ROUND_CEILING));
            }
            if (outboundSummaryDO.getCostPriceWithTax() != null) {
                outboundSummaryDO.setCostPriceWithoutTax(outboundSummaryDO.getCostPriceWithTax().divide(new BigDecimal("1.13"), 2, BigDecimal.ROUND_CEILING));
            }
            OutboundSummaryVO outboundSummaryVO = new OutboundSummaryVO();
            BeanUtils.copyProperties(outboundSummaryDO, outboundSummaryVO);
            outboundSummaryVOs.add(outboundSummaryVO);
        }
        pageCommonVO.setPageInfoList(outboundSummaryVOs);
        return pageCommonVO;
    }

    public void export(OutboundSummaryConditionVO condition, String orgId) throws Exception {
        condition.setOrgId(orgId);
        List<OutboundSummaryDO> doList = outboundSummaryMapper.list(condition);
        String title = "备件出库汇总表";
        String[] rowsName = new String[]{"序号", "NC客户编码/供应商编码", "PMS客户代码/供应商编码", "客户名称/供应商名称", "客户/供应商分类", "出库单号", "出库类别", "销售订单", "采购退货单号", "三包备件指令单号",
                "预留单号", "仓库", "库区", "制单人", "创建时间", "出库时间", "零件编号", "零件名称", "计量单位", "数量", "单价", "销售总额", "实际开票金额（含税）", "实际开票金额（不含税）", "计划价", "全国统一价",
                "含税成本价", "未含税成本价", "备注"};
        List<Object[]> dataList = new ArrayList<Object[]>();
        Object[] objs;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < doList.size(); i++) {
            OutboundSummaryDO e = doList.get(i);
            e.setBillAmountWithTax(e.getSellAmount());
            if (e.getBillAmountWithTax() != null) {
                e.setBillAmountWithoutTax(e.getBillAmountWithTax().divide(new BigDecimal("1.13"), 2, BigDecimal.ROUND_CEILING));
            }
            if (e.getCostPriceWithTax() != null) {
                e.setCostPriceWithoutTax(e.getCostPriceWithTax().divide(new BigDecimal("1.13"), 2, BigDecimal.ROUND_CEILING));
            }
            objs = new Object[rowsName.length];
            objs[0] = i + 1;
            objs[1] = e.getCustomNcCode();
            objs[2] = e.getCustomPmsCode();
            objs[3] = e.getCustomName();
            objs[4] = e.getCustomType();
            objs[5] = e.getOutBillNo();
            objs[6] = e.getOutType();
            objs[7] = e.getSellBillNo();
            objs[8] = e.getReturnBillNo();
            objs[9] = e.getOrderBillNo();
            objs[10] = e.getReservedBillNo();
            objs[11] = e.getWarehouseCode();
            objs[12] = e.getAreaCode();
            objs[13] = e.getCreateName();
            objs[14] = e.getCreateTime()==null?"":sdf.format(e.getCreateTime());
            objs[15] = e.getOutTime()==null?"":sdf.format(e.getOutTime());
            objs[16] = e.getMaterialsNo();
            objs[17] = e.getMaterialsDesc();
            objs[18] = e.getUnit();
            objs[19] = e.getQuantity();
            objs[20] = e.getPrice();
            objs[21] = e.getSellAmount();
            objs[22] = e.getBillAmountWithTax();
            objs[23] = e.getBillAmountWithoutTax();
            objs[24] = e.getPlanPrice();
            objs[25] = e.getFlatPrice();
            objs[26] = e.getCostPriceWithTax();
            objs[27] = e.getCostPriceWithoutTax();
            objs[28] = e.getRemark();
            dataList.add(objs);
        }
        ExportUtil exportUtil = new ExportUtil(title, rowsName, dataList);
        exportUtil.export(response);
    }
}
